package com.cyms.modules.base.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.cyms.common.annotation.SysLog;
import com.cyms.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.cyms.modules.base.entity.CarEntity;
import com.cyms.modules.base.entity.CompanyEntity;
import com.cyms.modules.base.service.CarService;
import com.cyms.modules.base.service.CompanyService;
import com.cyms.modules.oss.cloud.OSSFactory;
import com.cyms.modules.oss.entity.SysOssEntity;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cyms.common.exception.RRException;
import com.cyms.common.utils.PageUtils;
import com.cyms.common.utils.R;



/**
 * 车辆档案
 *
 * @author low
 * @email tolower@foxmail.com
 * @date 2018-12-03 21:58:41
 */
@RestController
@RequestMapping("base/car")
public class CarController extends AbstractController {
    @Autowired
    private CarService carService;
    @Autowired
    private CompanyService companyService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("base:car:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("createOrg",getUser().getCompanyId());
        PageUtils page = carService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/select")
    @RequiresPermissions("base:car:list")
    public R select() {
    	List<CompanyEntity> companyList=companyService.selectList(new EntityWrapper<CompanyEntity>().eq("create_org",getUser().getCompanyId()));
    	List<Map<String,Object>> list = companyList.parallelStream().map(company->{
    		Map<String,Object> map=new HashMap<>();
    		map.put("label", company.getName());
    		map.put("value", company.getId());
    		//
    		List<CarEntity> carList=carService.selectList(new EntityWrapper<CarEntity>().eq("company_id", company.getId()));
    		map.put("children", carList.parallelStream().map(car->{
    			Map<String,Object> data=new HashMap<>();
    			data.put("label", car.getCode());
    			data.put("value", car.getId());
    			data.put("code", car.getCode());
    			data.put("driver", car.getDriverName());
    			data.put("seatNum", car.getSitNum());
    			data.put("type", car.getType());
    			return data;
    		}).collect(Collectors.toList()));
    		return map;
    	}).collect(Collectors.toList());
    	return R.ok().put("carOptions", list);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("base:car:info")
    public R info(@PathVariable("id") Long id){
			CarEntity car = carService.selectById(id);

        return R.ok().put("car", car);
    }

    /**
     * 保存
     */
    @SysLog("保存车辆档案")
    @RequestMapping("/save")
    @RequiresPermissions("base:car:save")
    public R save(@RequestBody CarEntity car){
    	car.setCreateDate(new Date());
    	car.setCreateUser(getUserId());
    	car.setCreateDept(getUser().getDeptId());
    	car.setCreateOrg(getUser().getCompanyId());
        carService.insert(car);

        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改车辆档案")
    @RequestMapping("/update")
    @RequiresPermissions("base:car:update")
    public R update(@RequestBody CarEntity car){
			carService.updateById(car);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("base:car:delete")
    public R delete(@RequestBody Long[] ids){
			carService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }
    /**
	 * 上传文件
	 */
	@PostMapping("/upload")
	@RequiresPermissions("base:car:update")
	public R upload(@RequestParam("file") MultipartFile file) throws Exception {
		if (file.isEmpty()) {
			throw new RRException("上传文件不能为空");
		}

		//上传文件
//		String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
		String url = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo_top_86d58ae1.png";//OSSFactory.build().uploadSuffix(file.getBytes(), suffix);

		

		return R.ok().put("url", url);
	}
}
